<?php


/*

	Copyright © 2011 Mike Bennett <mike@kereru.com>

	This file is part of Tazu-Time.

    Tazu-Time is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Tazu-Time is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tazu-Time.  If not, see <http://www.gnu.org/licenses/>.
    
*/


//$e = get_loaded_extensions();
//array_dump($e);

class settings extends db {


	function __construct($data_file=FALSE) {
		parent::__construct($data_file);
		$this->getTextTranslations();			// into $GLOBALS['xlate']
	}

	
	/* Check database structure */
	function database_check() {
		global $gl;

		if ( ! file_exists( $this->db_path.$this->db_file.$this->db_ext ) ) {

//wb_message_box(null,$this->dsn);

			$db_ver = '0.9';
			
			
			try {
				
				$db = new PDO($this->dsn);
	
				// status
				$qry = "CREATE TABLE status(
					id INTEGER PRIMARY KEY AUTOINCREMENT,
				    param  TEXT,
				    value TEXT
				)";
								
				$db->exec($qry);
	
	
				// Text string translations.
				$qry = "CREATE TABLE xlate(
					id INTEGER PRIMARY KEY AUTOINCREMENT,
				    code TEXT,
				    default_txt  TEXT,
				    replace  TEXT
				)";
				$db->exec($qry);


				$qry = "INSERT INTO xlate (code,default_txt) values ('customer','Customer');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('customers','Customers');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('project','Project');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('projects','Projects');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('session','Session');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('sessions','Sessions');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('time','Time');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('duration','Duration');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('worker','Worker');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('workers','Workers');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('comment','Comment');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('report','Report');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('reports','Reports');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('customers.name','Customer');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('customers.company','Company Name');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('customers.email','Bill Email');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('estimate_hh_mm','Estimate hh:mm');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('tasks.task_code','Project Code');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('tasks.task','Description');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('tasks.estimate_time','Estimate');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('tasks.time','Worked');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('tasks.estimate_warning','Warn');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('tasks.charged','Billable');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('tasks.period','Period');";
				$db->exec($qry);

				$qry = "INSERT INTO xlate (code,default_txt) values ('tasks.rate','Rate');";
				$db->exec($qry);

				// Entry data definitions
//			    tag TEXT,								// column name for data xfer, csv reports etc
//			    name TEXT,								// field/data name
//			    unit TEXT,								// displayed unit string
//			    description BLOB						// Description/Instructions

				$qry = "CREATE TABLE dat_types(
					id INTEGER PRIMARY KEY AUTOINCREMENT,
				    tag TEXT,
				    name TEXT,
				    unit TEXT,
				    description BLOB
				)";
				$db->exec($qry);

				$qry = "INSERT INTO dat_types (tag,name,description,unit) values ('distance','Mileage','Travel by vehicle.','KM');";
				$db->exec($qry);

				$qry = "INSERT INTO dat_types (tag,name,description,unit) values ('tolls','Phone Number','Phone number called. Toll cost recovery.','');";
				$db->exec($qry);
				
				$qry = "INSERT INTO dat_types (tag,name,description,unit) values ('stock_code','Stock Item','Stock item code used for this task.','');";
				$db->exec($qry);
				
				$qry = "INSERT INTO dat_types (tag,name,description,unit) values ('expense','Expense','Miscellaneous expenses.','$');";
				$db->exec($qry);
				
				$qry = "INSERT INTO dat_types (tag,name,description,unit) values ('taxi','Taxi','Taxi cost.','$');";
				$db->exec($qry);
				
	
				$qry = sprintf("INSERT INTO status (param,value) VALUES ('db_ver','%s');",$db_ver);
				$db->exec($qry);


				$db = null;
				
			} catch (PDOException $e) {
			   $this->error($e->getMessage());
				return;
			}

		}
		


		// Current database version
/*

		$this->db_ver = $this->getDBVersion();

		if ( $this->db_ver == 0.9 ) {

			// Upgrade 0.9 -> 1.0
			$db_ver = '1.0';

			try {
			
				$db = new PDO($this->dsn);
	
				$qry = "ALTER TABLE customers ADD email TEXT;";
				$db->exec($qry);

				$qry = "ALTER TABLE tasks ADD COLUMN estimate_time INTEGER DEFAULT NULL";
				$db->exec($qry);

				$qry = "ALTER TABLE tasks ADD COLUMN estimate_warning INTEGER DEFAULT NULL";
				$db->exec($qry);

				$qry = sprintf("UPDATE status set value='%s' where param='db_ver'",$db_ver);
				$db->exec($qry);

				$this->db_ver = $db_ver;
	
				$db = null;
				
	
			} catch (PDOException $e) {
			   $this->error($e->getMessage());
				return;
			}


		}

*/
	}
	




	// --- text Translations

	function Xlate($label) {

		if ( $GLOBALS['xlate'] ) {
			foreach ( $GLOBALS['xlate'] as $code => $replacement ) {
				$label = str_replace('[[['.$code.']]]',strtoupper($replacement),$label);
				$label = str_replace('[['.$code.']]',strtolower($replacement),$label);
				$label = str_replace('['.$code.']',$replacement,$label);
			}
		}

		return($label);	
	}


	function addDat($tag,$name,$unit,$description) {
		$qry = sprintf("INSERT INTO dat_types (tag,name,unit,description) VALUES ('%s','%s','%s','%s');",
						$tag,$name,$unit,$description);
		return( $this->insert($qry) );
	}

	function delDat($id) {
		$qry = sprintf("DELETE FROM dat_types WHERE id='%s'",$id);
		return( $this->execute($qry));		
	}


	function getDatName($id) {
		return( $this->col($id,'dat_types','name') );
	}

	function getDatTag($id) {
		return( $this->col($id,'dat_types','tag') );
	}

	function getDatUnit($id) {
		return( $this->col($id,'dat_types','unit') );
	}

	function getDatDescription($id) {
		return( $this->col($id,'dat_types','description') );
	}


	function getTextTranslations() {

		
		$qry = sprintf("SELECT * FROM xlate;",$entry_id);
		$result = array();
		$rows = $this->rows($qry,array('id','code','customer','default_txt','replace'));

		$result = array();
		foreach ( $rows as $idx => $row ) {
			if ( trim($row['replace']) == '' ) {
				// Default
				$result[strtolower($row['code'])] = trim($row['default_txt']);
			}
			else {
				$result[strtolower($row['code'])] = trim($row['replace']);
			}
		}
		$GLOBALS['xlate'] = $result;
		
		return( $result );

	}

	function getDataTypes() {
		$qry = sprintf("SELECT * FROM dat_types;");
		$result = array();
		$rows = $this->rows($qry,array('id','tag','name','type','unit','description'));

		$result = array();
		foreach ( $rows as $idx => $dat ) {
			$result[$dat['id']] = $dat;
		}

		return( $result );

	}


	function getXlateDefault($id) {
		return( $this->col($id,'xlate','default_txt') );
	}

	function getXlateReplace($id) {
		return( $this->col($id,'xlate','replace') );
	}

	function getXlate() {
		$qry = sprintf("SELECT * FROM xlate;");
		$result = array();
		$rows = $this->rows($qry,array('id','default_txt','replace'));

		$result = array();
		foreach ( $rows as $idx => $dat ) {
			$result[$dat['id']] = $dat;
		}

		return( $result );

	}



	function updateDat($id,$col,$val) {
		$qry = sprintf("UPDATE dat_types SET %s='%s' WHERE id='%s';",$col,$val,$id);
		return( $this->execute($qry));		
	}


	function updateXlate($id,$col,$val) {
		$qry = sprintf("UPDATE xlate SET %s='%s' WHERE id='%s';",$col,$val,$id);
		return( $this->execute($qry));		
	}



}

?>